//
//  LIUListCell.m
//  EasyNotes
//
//  Created by 劉裕 on 25/5/2016.
//  Copyright © 2016年 劉裕. All rights reserved.
//

#import "LIUListCell.h"
#import <Masonry/Masonry.h>
#import "LIUNote.h"
#import "UIImageView+WebCache.h"
#import "NSDate+Component.h"

@interface LIUListCell ()
@property (nonatomic, weak) UILabel *titleLbl;
@property (nonatomic, weak) UILabel *createDateLbl;
@property (nonatomic, weak) UIImageView *image;

@end

@implementation LIUListCell

+ (instancetype)cellWithTableView:(UITableView *)tableView reuseId:(NSString *)reuseId {
    LIUListCell *listCell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (nil == listCell) {
        listCell = [[LIUListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }
    return listCell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    UILabel *titleLbl = [[UILabel alloc] init];
    [self addSubview:titleLbl];
    self.titleLbl = titleLbl;
    
    UILabel *createDateLbl = [[UILabel alloc] init];
    createDateLbl.font = [UIFont systemFontOfSize:12.0f];
    createDateLbl.textColor = LIUColor(148, 148, 148, 1.0f);
    [self addSubview:createDateLbl];
    self.createDateLbl = createDateLbl;
    
    UIImageView *image = [[UIImageView alloc] init];
    [self addSubview:image];
    self.image = image;
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    WS(ws)
    [self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(ws).offset(20);
        make.left.equalTo(ws).offset(15);
    }];
    [self.createDateLbl mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(ws.titleLbl.mas_bottom).offset(10);
        make.left.equalTo(ws.titleLbl);
    }];
    [self.image mas_makeConstraints:^(MASConstraintMaker *make) {

        make.size.mas_equalTo(CGSizeMake(40, 40));
        make.top.equalTo(ws).offset(20);
        make.right.equalTo(ws).offset(20);
        make.bottom.equalTo(ws).offset(-20);
    }];
    
}

- (void)setViewModel:(LIUNote *)viewModel {
    _viewModel = viewModel;
    self.titleLbl.text = viewModel.noteTitle;
    self.createDateLbl.text = [viewModel.noteCreateDate dateComponent];
    [self.image sd_setImageWithURL:[NSURL URLWithString:viewModel.noteImageName] placeholderImage:nil];
}

/**
 *  重写drawRect:方法自定义cell分割线
 */
- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context, rect);

    CGContextSetStrokeColorWithColor(context, LIUColor(150, 150, 153, 0.3).CGColor);
    CGContextStrokeRect(context, CGRectMake(10, rect.size.height, rect.size.width - 10, 1));
}
@end
